#include <stdio.h>

int bit_1_1(unsigned int x)
{
    int cnt;
    
    cnt = 0;
    while (x) {
        if (x & 1)
            cnt ++;
        x >>= 1;
    }
    
    return cnt;
}

/* like merge */
int bit_1_2(unsigned int x)
{
    x = (x&0x55555555) + ((x>>1)&0x55555555);    
    x = (x&0x33333333) + ((x>>2)&0x33333333);
    x = (x&0x0f0f0f0f) + ((x>>4)&0x0f0f0f0f);
    x = x + (x>>8);       /* 8-bits int > 32 */
    x = x + (x>>16);

    return x & 0x0000003f;
}

int main(int argc, char *argv[])
{
    int i = 0x10011101;
    
    printf("there are %d bits is 1 in 0x%x\n", bit_1_1(i), i);
    printf("there are %d bits is 1 in 0x%x\n", bit_1_2(i), i);

    return 0;
}
